const defines = require('./../defines');

let _playerList = [];

const Player = function (socket, data) {
    let that = {};

    let _socket = socket;
    let _uid = data.uid;
    let _nickName = data.nickName;
    let _avatarUrl = data.avatarUrl;
    let _diamonds = data.diamonds;                // 钻石

    let _attribute = {
        grade: 0,                   // 等级
        blood: 0,                   // 血
        // 蓝
        // 速度
        // 魔法
        // 攻击
        gold: 0,                    // 金
        wood: 0,                    // 木
        water: 0,                   // 水
        fire: 0,                    // 火
        soil: 0                     // 土
    };


    // 发送消息的封装
    const notify = function (msg, index, data) {
        _socket.emit('notify', {msg: msg, callBackIndex: index, data: data});
    };

    // get和set的方法封装
    // 数据名，（get, set 或 both）,要修改或获得的值得变量
    const setDefineProperty = function (property, type, value) {
        switch (type) {
            case defines.get:{
                Object.defineProperty(that, property, {
                    get: function () {
                        return value;
                    }
                });
                break;
            }
            case defines.set:{
                Object.defineProperty(that, property, {
                    set: function (val) {
                        value = val;
                    }
                });
                break;
            }
            case defines.both:{
                Object.defineProperty(that, property, {
                    get: function () {
                        return value;
                    },
                    set: function (val) {
                        value = val;
                    }
                });
                break;
            }
            default:{
                break;
            }
        }
    };


    notify('login', data.callBackIndex, {
        createrplayer: 'ok',
        gameID: _uid,
        avatarUrl: _avatarUrl,
    });

    // 处理接受的消息
    _socket.on('notify', function (res) {
       let msg = res.msg;
       let callBackIndex = res.callBackIndex;
       let data = res.data;
       switch (msg) {
           case 'createrplayer': { // 创建角色
               console.log('创建角色成功');
               // 发送玩家数据
               notify('createrplayer', callBackIndex, {});
           }
           case 'walk': { // 行走
               break;
           }
           default: {
               break;
           }
       }
    });

    return that;
};

exports.createrPlayer = function (socket, data) {
    let player = Player(socket, data);
    _playerList.push(player);
};